package com.zr.depotarea.service.impl;

import com.zr.depotarea.mapper.DepotareaMapper;
import com.zr.depotarea.pojo.*;
import com.zr.depotarea.service.DepotareaService;
import com.zr.meiju.StatusEnum;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by star on 2019/7/16.
 */
@Service
public class DepotareaServiceImpl implements DepotareaService {
    @Autowired
    private DepotareaMapper depotareaMapper;
    /**
     *
     * 库区属性----分页
     */
    @Override
    public ResultVo<AllRecords> queryPage(DepotareaSelectVo depotareaSelectVo) {
        //查询数据
        List<Depotarea> plantClassList = depotareaMapper.queryPage(depotareaSelectVo);
        //查询数量
        int count =  depotareaMapper.queryCount(depotareaSelectVo);
        //把当前页、每页大小、总页数、总条数、数据统一放入到AllRecords返回
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(depotareaSelectVo.getPageIndex());
        allRecords.setPageSize(depotareaSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(plantClassList);
        return ResultBuildVo.success(allRecords);
    }
    /**
     * 库区属性----新增
     *
     */
    @Override
    public ResultVo addDepotarea(DepotareaAddVo depotareaAddVo) {
        //逻辑验证
        ResultVo assigment = assigment(depotareaAddVo);
        if (!assigment.isSuccess()){
            return assigment;
//            return ResultBuildVo.error(assigment.getErrorMessage(),assigment.getErrorCode());
        }
        //给数据库中的创建时间和修改时间等信息赋值
        Depotarea depotarea = new Depotarea();
        Date nowDate = new Date();
        depotarea.setCreateTime(nowDate);
        depotarea.setCreateName("小明");
        depotarea.setUpdateTime(nowDate);
        depotarea.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(depotareaAddVo,depotarea);
        depotareaMapper.addDepotarea(depotarea);
        return ResultBuildVo.success(depotareaAddVo);
    }
    /**
     * 库区属性-----修改
     * @param depotareaUpdateVo
     * @return
     */
    @Override
    public ResultVo updateDepotarea(DepotareaUpdateVo depotareaUpdateVo) {
        //逻辑验证=禁用状态下才允许修改
        ResultVo assigment = assigmentUpdate(depotareaUpdateVo);
        if (!assigment.isSuccess()){
            return assigment;
        }
        //给数据库中的创建时间和修改时间等信息赋值
        Depotarea depotarea = new Depotarea();
        Date nowDate = new Date();
        depotarea.setUpdateTime(nowDate);
        depotarea.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(depotareaUpdateVo, depotarea);
        depotareaMapper.updateDepotarea(depotarea);
        return ResultBuildVo.success(depotareaUpdateVo);
    }
    /**
     *
     * 修改状态
     * @param
     * @return
     */
    @Override
    public ResultVo updateStatus(DepotareaStatusVo depotareaStatusVo) {
        Depotarea depotarea = new Depotarea();
        Date nowDate = new Date();
        depotarea.setUpdateTime(nowDate);
        depotarea.setUpdateName("花花");
        BeanUtils.copyProperties(depotareaStatusVo, depotarea);
        depotareaMapper.updateStatus(depotarea);
        return ResultBuildVo.success(depotareaStatusVo);

    }

    public ResultVo assigment(DepotareaAddVo depotareaAddVo){
        //        1.验证编码是否已经存在
        List<Depotarea> subClassesCode = depotareaMapper.queryByCode(depotareaAddVo.getAreacode());
        if (!CollectionUtils.isEmpty(subClassesCode)){
            return ResultBuildVo.error("已经存在编码！","500");
        }
//        1.验证名称是否已经存在
        List<Depotarea> subClassesName = depotareaMapper.queryByName(depotareaAddVo.getAreaname());
        if (!CollectionUtils.isEmpty(subClassesName)){
            return ResultBuildVo.error("已经存在名称！","500");
        }
        return ResultBuildVo.success(depotareaAddVo);
    }
    public ResultVo assigmentUpdate(DepotareaUpdateVo depotareaUpdateVo){
        //验证数据是否存在
        List<Depotarea> depotareas = depotareaMapper.queryById(depotareaUpdateVo.getId());
        if (CollectionUtils.isEmpty(depotareas)){
            return ResultBuildVo.error("修改数据不存在！","500");
        }
        //1.验证名称是否已经存在
        List<Depotarea> depotareasList = depotareaMapper.queryByAll(depotareaUpdateVo);
        if (!CollectionUtils.isEmpty(depotareasList)){
            return ResultBuildVo.error("已经存在名称！","500");
        }
        Depotarea depotarea = new Depotarea();
        //禁用状态下才允许修改
        if (depotarea.getAreaStatus()== StatusEnum.QIYONG.getStatusValue()){
            return ResultBuildVo.error("禁用状态才能修改！","500");
        }
        return ResultBuildVo.success(depotareaUpdateVo);
    }







}
